<template>
  <div class="login-container">
    <div class="login-form">
      <img class="login-logo" src="@/assets/images/logo.595745bd.png" alt="">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <el-form-item prop="loginName">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.loginName"
            placeholder="请输入账号"
            type="text"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-form-item prop="code" class="code-container">
          <div class="code-input">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              ref="code"
              v-model="loginForm.code"
              type="nubmer"
              placeholder="请输入验证码"
            />
          </div>
          <div class="code"><img ref="codeImg" src="" alt="" @click="getCodeImg"></div>
        </el-form-item>

        <el-button class="login-btn" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="onLogin">登录</el-button>

      </el-form>
    </div>
  </div>
</template>

<script>
import { codeImgAPI } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        mobile: null,
        loginType: 0,
        account: null
      },
      loginRules: {
        loginName: [
          { required: true, trigger: 'blur', message: '账号不能为空' },
          { pattern: /^[a-zA-Z0-9_-]{4,16}$/, trigger: 'blur', message: '账号格式不正确' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
          { min: 5, max: 16, trigger: 'blur', message: '密码格式不正确' }
        ],
        code: [
          { required: true, trigger: 'blur', message: '验证码不能为空' },
          { min: 4, max: 4, trigger: 'blur', message: '验证码格式不正确' }
        ]
      },
      passwordType: 'password',
      loading: false
    }
  },
  created() {
    this.getCodeImg()
  },
  methods: {
    async onLogin() {
      try {
        // 先验证表单是否验证通过
        await this.$refs.loginForm.validate()
        this.loading = true

        // 发送登录请求
        const res = await this.$store.dispatch('user/login', {
          ...this.loginForm,
          clientToken: this.$store.state.user.clientToken
        })
        if (res === 'ok') {
          this.$router.push('/')
        } else {
          return new Error(res)
        }
      } catch (error) {
        this.$message(error.message)
        this.getCodeImg()
      } finally {
        this.loading = false
        this.$refs.loginForm.resetFields()
      }
    },
    async getCodeImg() {
      try {
        const res = await codeImgAPI()
        this.$refs.codeImg.src = res.request.responseURL
      } catch (error) {
        // 验证码获取失败
        this.$message('获取验证码失败，请刷新重新获取')
      }
    },
    showPwd() {
      this.passwordType = this.passwordType ? '' : 'password'
      this.$nextTick(() => {
        // 如果立刻去执行我们拿到的是未更新的dom元素绑定的focus，等到更新执行后就没有focus
        this.$refs.password.focus()
      })
    }
  }
}
</script>

<style lang="scss">
$bg:#283443;
$light_gray:#999;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    width: 100%;
    height: 52px;
    margin-bottom: 24px;
    background: #fff;
    border: 1px solid #e2e2e2;
    border-radius: 4px;
  }
  .el-form-item__content{
    line-height: 40px;
    position: relative;
    font-size: 14px;
    height: 52px;
  }
  .code-container{
    .el-form-item__content{
      display: flex;
      align-items: center;
      .code-input{
        width: 70%;
      }
      .code{
        width: 30%;
        img{
          width: 100%;
          height: 100%;
          vertical-align: middle;
        }
      }
    }
  }

}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#f56c6c;

.login-container {
   position: relative;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background-image: url("~@/assets/images/logo-64.png");
    background-repeat: no-repeat;
    background-size: cover;
  .login-logo{
  position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
    z-index: 99;
    }
  .login-form {
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }
  .login-btn {
        width: 100%;
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
}
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

</style>
